package org.zfes.snowier.common.monitor.http.response;

import java.io.Serializable;
import java.util.function.Consumer;

public class JsonResponse implements Serializable{
			
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String json;
	
    private int statusCode;
    
    private String error;
    
    public JsonResponse() {
    }
    public JsonResponse(int statusCode) {
        this.statusCode = statusCode;
    }
    public JsonResponse(String json, int statusCode) {
        this.json = json;
        this.statusCode = statusCode;
    }
	public String getJson() {
		return json;
	}
	public JsonResponse setJson(String json) {
		this.json = json;
		return this;
	}
	public int getStatusCode() {
		return statusCode;
	}
	public JsonResponse setStatusCode(int statusCode) {
		this.statusCode = statusCode;
		return this;
	}
	public String getError() {
		return error;
	}
	public JsonResponse setError(String error) {
		this.error = error;
		return this;
	}
	
	public boolean isok() {
        return 200==statusCode;
    }
	
	public <T> void isok(Consumer<? super String> consumer) {
        if (200==statusCode) {
        	 consumer.accept(json);
        }
    }
	 
	
	
	 public JsonResponse orElse(Consumer<? super JsonResponse> consumer) {
		 if (200!=statusCode) {
        	 consumer.accept(this);
        }
		 return this;
	 }
	 public  JsonResponse orElseThrow(Exception e)  {
		 try {
			 if (200!=statusCode) {
				 throw e;
	        }
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		 return this;
	 }
	 
    
}
